import { MessageBox } from 'element-ui';
import { Message } from "element-ui";
import store from "@/store";
import request from '@/utils/request';
/**
 * 上传附件
 * @param {json} params 文件参数
 */
export function uploadFile(params) {
  return request({
    url: process.env.VUE_APP_FILE_URL + '/file/upload',
    method: 'post',
    data: params
  });
}

export function alertMessage(text, type = 'success') {
  Message({
    message: text,
    type: type,
    duration: 2000
  });
}

// confirm提示框
export function alertWithCallback(text, title, type = 'warning') {
  return MessageBox.confirm(text, title, {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: type
  });
}

export function showAlertMessage(title, params) {
  const message = []
  for (var key in params) {
    message.push(`"${params[key]}"`);
  }
  const alertMsg = '<div>' + title + '<div>' +
    '<div style="color:#0F327D" class="aq-row-wrap">' + message.join('&nbsp;&nbsp;&nbsp;') + '</div>'
  return alertMsg;
}

// confirm HTML提示框
export function alertHtmlWithCallback(html, title, confirmButtonText = "确认提交") {
  return MessageBox.confirm(html, title, {
    confirmButtonText: confirmButtonText,
    cancelButtonText: '取消',
    dangerouslyUseHTMLString: true
  });
}

// 非空校验
export function isValidateNull(rule, value, callback) {
  if (value || value == 0) {
    if (value != '') {
      callback();
    } else {
      callback(new Error("此项为必填项"));
    }
  } else {
    callback(new Error("此项为必填项"));
  }
}

let result = [];
let mappingObj = {
  finish: false
};

export function findCodes(menus, router) {
  for (const data of menus) {
    if (data.path.indexOf(router) != -1 || data.path.indexOf('/' + router) != -1) {
      result = data.keyCodes || data.KeyCodes;
    } else if (data.children && data.children.length > 0) {
      findCodes(data.children, router);
    }
  }
  return result;
}

// 获取第一条路由
export function getFirstRoute(routes, routePath) {
  routes[0].children.forEach(ele => {
    if (ele.children.length !== 0) {
      this.getFirstRoute(routes[0].children)
    }
    routePath = routePath + '/' + ele.path
  })
  return routePath
}

// 获取 mapping menu obj
export function getMenuMappingObject() {
  if (mappingObj.finish == false) {
    const menus = store.getters.permission_routes;
    for (const data of menus) {
      // if (data.path === router || data.path === ('/' + router)) {
      let path = data.path.replace('/', '');
      if (!mappingObj[path]) {
        mappingObj[path] = data.name;
      }
      if (data.children) {
        for (const child of data.children) {
          let cPath = child.path.replace('/', '');
          if (!mappingObj[cPath]) {
            mappingObj[cPath] = child.name;
          }
        }
      }
    }
    mappingObj.finish = true;
  }
  return mappingObj;
}

// 补齐前缀
export function prefixInteger(num, length) {
  return ("0000000000000000" + num).substr(-length);
}

/*
** randomWord 产生任意长度随机字母数字组合
** randomFlag-是否任意长度 min-任意长度最小位[固定位数] max-任意长度最大位
*/
export function randomPassword(randomFlag, min, max) {
  let result = '';
  let range = min;
  const charset = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

  // 随机产生
  if (randomFlag) {
    range = Math.round(Math.random() * (max - min)) + min;
  }
  for (let i = 0; i < range; i++) {
    const random = Math.round(Math.random() * (charset.length - 1));
    result += charset[random];
  }
  return result;
}

/**
 * @returns {string}
 */
export function createUniqueString() {
  const timestamp = +new Date() + '';
  const randomNum = parseInt((1 + Math.random()) * 65536) + '';
  return (+(randomNum + timestamp)).toString(32);
}

// 组织架构树，最后一项children:[]的处理
export function getTreeData(data) {
  for (let i = 0; i < data.length; i++) {
    if (data[i].children.length < 1) {
      // children若为空数组，则将children设为undefined
      data[i].children = undefined;
    } else {
      // children若不为空数组，则继续 递归调用 本方法
      getTreeData(data[i].children);
    }
  }
  return data;
}

export function securityPhone(phone) {
  let ValidatePhone = /^1[1,2,3,4,5,6,7,8,9,0]\d{9}$/;
  if (ValidatePhone.test(phone)) {
    const nameString = phone.substring(0, 3) + "****" + phone.substr(phone.length - 4)
    return nameString;
  } else {
    return false;
  }
}